/*
 * GUIComponent.h
 *
 *  Created on: Jul 15, 2010
 *      Author: Ry
 */

#ifndef COMPONENT_H_
#define COMPONENT_H_

#include <iostream>
#include <vector>

#include "gui/GUIEvent.h"
#include "art/Rect.h"
#include "art/RGBA.h"

/**
 * Base class for all components
 */
class Component {
protected:
	bool focus;
	bool hover;
	Rect rect;
	RGBA backgroundColor;
	RGBA borderColor;

	bool drawBorder;

	Component *parent;
	std::vector< Component* > children;
public:
	Component() {
		parent = NULL;
		backgroundColor = RGBA(0x666666, 1.0f);
		borderColor = RGBA(0xcccccc, 1.0f);
		drawBorder = true;

		focus = false;
		hover = false;
	}

	/**
	 * Adds a child component to this component
	 */
	Component* add(Component *component) {
		component->setParent(this);

		int x = rect.x + 5;
		int y = rect.y + 5;
		int width = rect.width - 10;

		for (unsigned int i = 0; i < children.size(); i++) {
			y += children[i]->rect.height + 5;
		}

		component->rect.x = x;
		component->rect.y = y;
		component->rect.width = width;

		children.push_back(component);

		return component;
	}

	void centerAt(int x, int y) {
		rect.centerAt(x, y);
		// TODO: Update the positions of the children
	}

	/**
	 * Sets the position of the component
	 */
	void positionAt(int x, int y) {
		rect.positionAt(x, y);
		// TODO: Update the positions of the children
	}

	/**
	 * Resizes the component
	 */
	void resize(unsigned int width, unsigned int height) {
		rect.width = width;
		rect.height = height;
		for (unsigned int i = 0; i < children.size(); i++) {
			children[i]->resize(width - 10, height - 10);
		}
	}

	/**
	 * Draws the component
	 */
	virtual void draw() = 0;

	/**
	 * Handles an event that has been sent to this component
	 */
	virtual void handleEvent(GUIEvent *event) = 0;

    virtual ~Component() {
		for (unsigned int i = 0; i < children.size(); i++) {
			delete children[i];
		}
	}

    bool getDrawBorder() const
    {
        return drawBorder;
    }

    void setDrawBorder(bool drawBorder)
    {
        this->drawBorder = drawBorder;
    }

	RGBA getBackgroundColor() const {
		return backgroundColor;
	}

	RGBA getBorderColor() const {
		return borderColor;
	}

	void setBackgroundColor(RGBA backgroundColor) {
		this->backgroundColor = backgroundColor;
	}

	void setBorderColor(RGBA borderColor) {
		this->borderColor = borderColor;
	}

	std::vector<Component*> getChildren() const {
		return children;
	}

	bool getFocus() const {
		return focus;
	}

	bool getHover() const {
		return hover;
	}

	Component *getParent() const {
		return parent;
	}

	void setChildren(std::vector<Component*> children) {
		this->children = children;
	}

	void setFocus(bool focus) {
		this->focus = focus;
	}

	void setHover(bool hover) {
		this->hover = hover;
	}

	void setParent(Component *parent) {
		this->parent = parent;
	}

    Rect &getRect() {
		return rect;
	}

	void setRect(Rect rect) {
		this->rect = rect;
	}

};

#endif /* COMPONENT_H_ */
